from flask import Flask,render_template,jsonify,request
from flask_restful import Api
from flask_migrate import Migrate
from flask_cors import CORS
from flask_jwt import JWT, jwt_required, current_identity

from config import Config
from views import ModelResource,DataResource,UserResource,model_bp,data_bp,db,user_bp
from jwtconfig import authenticate,identity

app = Flask(__name__)

app.config.from_object(Config)
CORS(app, supports_credentials=True)
jwt = JWT(app, authenticate, identity)

db.init_app(app)
migrate = Migrate(app, db)
api = Api(app)



@app.route("/<int:id>")
def index(id):
    return render_template(f"index{id}.html")


api.add_resource(ModelResource, '/model', '/model/<int:id>')
api.add_resource(DataResource, '/data', '/data/<int:id>')
api.add_resource(UserResource, '/user', '/data/<int:id>')
model_bp.register_error_handler(404, lambda e: {'code': "404", "msg": 'Resource not found'}, )
app.register_blueprint(model_bp)
data_bp.register_error_handler(404, lambda e: {'code': "404", "msg": 'Resource not found'}, )
app.register_blueprint(data_bp)
user_bp.register_error_handler(404, lambda e: {'code': "404", "msg": 'Resource not found'}, )
app.register_blueprint(user_bp)



if __name__ == '__main__':
    app.debug = True
    app.run()

